package database;

import Entity.Employee;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author BinhNV_A04911
 */
public class EmployeeManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;
    
    public EmployeeManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(EmployeeManagerDB EmployeeManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetEmployee}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
                int EmployeeID = rs.getInt("ClassEmployeeID");
                String EmployeeName = rs.getString("ClassEmployeeName");
                int DepartmentID = rs.getInt("ClassDepartmentID");
                int AuthorID = rs.getInt("ClassAuthorID");
                 String Gender = rs.getString("ClassGender");
                 String Address = rs.getString("ClassAddress");
                 String Phone = rs.getString("ClassPhone");
                boolean status = rs.getBoolean("Status");
                Employee obj = new Employee(EmployeeID, EmployeeName, DepartmentID, AuthorID, Gender, Address, Phone, status);

                EmployeeManager.add(obj);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteEmployee(Employee obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteEmployee(?)}");
            cstmt.setInt(1, obj.getEmployeeID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertEmployee(Employee obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertEmployee(?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1,obj.getEmployeeID());
            cstmt.setString(2, obj.getEmployeeName());
            cstmt.setInt(3,obj.getDepartmentID());
            cstmt.setInt(4,obj.getAuthorID());
            cstmt.setString(5, obj.getGender());
            cstmt.setString(6, obj.getAddress());
            cstmt.setString(7, obj.getPhone());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateEmployee(Employee obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateEmployee(?,?,?)}");
            cstmt.setInt(1, obj.getClassRoomBlockID());
            cstmt.setString(2, obj.getClassRoomBlockName());
            cstmt.setBoolean(3, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
